package com.yidu.hbatsft.service.impl;

import com.github.pagehelper.PageHelper;
import com.yidu.hbatsft.dao.AtsBaseSportMapper;
import com.yidu.hbatsft.dao.AtsEmpExpertAndUserMapper;
import com.yidu.hbatsft.dao.AtsEmpExpertMapper;
import com.yidu.hbatsft.domain.AtsBaseSport;
import com.yidu.hbatsft.domain.AtsEmpExpert;
import com.yidu.hbatsft.domain.AtsEmpExpertVo;
import com.yidu.hbatsft.domain.AtsEmpLeader;
import com.yidu.hbatsft.service.AtsEmpExpertService;
import com.yidu.privilege.Dao.RoleDao;
import com.yidu.privilege.Dao.UserAndroleMapper;
import com.yidu.privilege.Dao.UserDao;
import com.yidu.privilege.Domain.Role;
import com.yidu.privilege.Domain.User;
import com.yidu.privilege.Domain.UserAndrole;
import com.yidu.utils.FileTools;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class AtsEmpExpertServiceimpl implements AtsEmpExpertService {

    @Resource
    AtsEmpExpertMapper aeem;

    @Resource
    AtsBaseSportMapper atsBaseSport;
    @Resource
    AtsBaseSportMapper baseSportMapper;
    @Resource
    AtsEmpExpertAndUserMapper atsEmpExpertAndUserMapper;
    @Resource
    UserDao userDao;

    @Resource
    RoleDao roleDao;
    @Resource
    UserAndroleMapper userAndroleMapper;
    @Resource
    FileTools fileTools;
    @Override
    public List<AtsEmpExpert> findAll(Integer pageNum, Integer pageSize,String expertName) {

        //开始的页数和行数
        PageHelper.startPage(pageNum,pageSize);

        return aeem.findAll(expertName);
    }

    @Override
    public List<AtsBaseSport> loadSprot() {
        return atsBaseSport.selectAll();
    }

    @Override
    public int findCout() {
        return  aeem.findCout();
    }

    /**
     * 根据ID查询用户
     * @param id
     * @return
     */
    @Override
    public AtsEmpExpertVo findById(Integer id) {


        return atsEmpExpertAndUserMapper.findByid(id);
    }

    /**
     * 无参数的查全部
     * @return
     */
    @Override
    public List<AtsEmpExpert> findAlls() {
        return aeem.findAlls();
    }

    @Override
    public int upOrInertExpertDate(AtsEmpExpertVo vo,MultipartFile file) {
        AtsEmpExpert ats = new AtsEmpExpert();//创建专家对象容器
        User user = new User();//创建用户对象同期

        ats.setExpertName(vo.getExpertName());//设置专家姓名
        ats.setAddr(vo.getAddr());//设置地址
        ats.setDeptName(vo.getDeptName()); //设置部门名称
        ats.setPostName(vo.getPostName());//设置岗位名称
        ats.setPhone(vo.getPhone());//设置电话
        //ats.setCreatTime(new Date());
        ats.setIsva(1);//设置为有效
        ats.setExpertSpecia(vo.getExpertSpecia());//设置专业
        //根据专业ID查询专业名称，并设置专业
        Integer sprotId = Integer.valueOf(vo.getExpertSpecia());
        AtsBaseSport sportinfo = baseSportMapper.findById(sprotId);
        //System.out.println(sportinfo.getSportName()+"===================项目名称");
        ats.setExpertExp(sportinfo.getSportName());//设置专业
        ats.setOranName(vo.getOranName());//设置所在公司
        //ats.setExpertExp(vo.getExpertExp());//设置查看到的专业
        ats.setRemark(vo.getRemark());//设置其他说明
        //System.out.println("备注说明："+vo.getRemark());
        user.setUsName(vo.getUsName());//设置登陆账号
        user.setUsPass(vo.getUsPass());//设置登陆密码
        user.setUsPhone(vo.getPhone());//设置电话
        user.setUsEmail(vo.getUsEmail());//设置Email邮箱
        user.setUsType("专家");//设置用户类型
        user.setUsState("0");//设置为有效
        user.setUsRemark(vo.getRemark());//设置备注说明
        //判断文件非空时则设置文件为用户头像
        if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {
            //处理异常
            try {
                String usPhoto = fileTools.saveFile("UserImg", file);//由工具类处理
                user.setUsPhoto(usPhoto);//设置头像
                System.err.println("===================" + usPhoto);
            } catch (IOException e) {//IOException e
                e.printStackTrace();//e.printStackTrace
            }
        }
        //不等于空则修改
        if(vo.getExpertId() !=null){
            ats.setExpertId(vo.getExpertId());//获取专家ID
            user.setUsId(vo.getUsId());//获取用户ID
            int cout = aeem.upExpertDate(ats);
            System.out.println("==============="+cout);
            if(cout>0){
               int c = userDao.update(user);
                System.out.println(c+":=====================c+userid:"+user.getUsId());
               if(c>0){
                   return cout;
               }
            }
        }else{
            //则是新增
            int cout = aeem.insert(ats);
            if(cout >0){
                user.setUsWaiid(ats.getExpertId());

                int c = userDao.insert(user);
                if(c>0){
                    UserAndrole userAndrole = new UserAndrole();

                    userAndrole.setUsId(user.getUsId());
                    Role role = roleDao.queryIdByExpertName();
                    userAndrole.setRoId(role.getRoId());
                    System.out.println("角色表ID："+role.getRoId());
                    userAndroleMapper.insert(userAndrole);
                    return cout;
                }
            }
        }

        return 0;
    }

    @Override
    public int deleteExpertDate(Integer id) {
        int cout = aeem.deleteExpertDate(id);
        if(cout>0){
            int c = userDao.deleteByExpertId(id);
            if(c>0){
                return cout;
            }
        }
        return 0;
    }

}
